//
//  SettingmatchViewController.m
//  PurchasingMeeting
//
//  Created by yanseng.lin on 9/3/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "SettingmatchViewController.h"
#import "DataManager.h"
#import "SizeObject.h"
#import "PMGlobal.h"

@interface SettingmatchViewController ()

@end

@implementation SettingmatchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setNavTitle:@"设置配比"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *img = [[PMGlobal getInstance]getretimage];
    [b setImage:img forState:UIControlStateNormal];
    b.frame = CGRectMake(10, 10, img.size.width, img.size.height);
    [b addTarget:self action:@selector(ret) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:b];
    self.navigationItem.leftBarButtonItem = left;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(search:) name:@"search" object:nil];
    
    UIImage *bgImage = [[UIHelper getInstance] getBundleImage:@"suit_bg.jpg"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:bgImage];
    
    
#ifdef DHH_iOS
    NumberInputView *input = [[NumberInputView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
    input.backgroundColor = [UIColor whiteColor];
    input.delegate = self;
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        input.bottom = 416+64;
    }
    else
    {
        input.bottom = 416;
    }

    input.tag = 180002;
    [self.view addSubview:input];
#else
    img = [[UIHelper getInstance] getBundleImage:@"detail_background.jpg"];
    UIImageView *background = [[UIImageView alloc]initWithImage:img];
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        background.frame = CGRectMake(0, 360+64, img.size.width+10, img.size.width);
    }
    else
    {
         background.frame = CGRectMake(0, 360, img.size.width+10, img.size.width);
    }
    background.userInteractionEnabled = YES;
    [self.view addSubview:background];
    
    NumberInputView *input = [[NumberInputView alloc]initWithFrame:CGRectMake(self.view.height-320, 360, 315, 342)];
    input.top = background.top+5;
    input.right = background.right-10;
    
    input.backgroundColor = [UIColor whiteColor];
    input.delegate = self;
    [self.view addSubview:input];
#endif
#ifdef DHH_iOS
    view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 216)];
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    UIImage *tintImage = [[UIHelper getInstance] getBundleImage:@"detail_tint.png"];
    tintImage = [tintImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
    
    tint = [[UIImageView alloc]initWithImage:tintImage];
    tint.width = 320;
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        tint.top = 64;
    }
    tint.userInteractionEnabled = YES;
    [view addSubview:tint];
    
    UIButton *clear = [UIButton buttonWithType:UIButtonTypeCustom];
    [clear setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"anniu_iphone.png"] forState:UIControlStateNormal];
    clear.frame = CGRectMake(0, 0, 70, 30);
    clear.center = CGPointMake(200, tint.height/2);
    [clear setTitle:@"清除配比" forState:UIControlStateNormal];
    clear.titleLabel.font = [UIFont systemFontOfSize:15];
    [clear addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
    [tint addSubview:clear];
    
    UIButton *save = [UIButton buttonWithType:UIButtonTypeCustom];
    [save setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"anniu_iphone.png"] forState:UIControlStateNormal];
    save.frame = CGRectMake(0, 0, 70, 30);
    save.center = CGPointMake(275, tint.height/2);
    [save setTitle:@"保存配比" forState:UIControlStateNormal];
    save.titleLabel.font = [UIFont systemFontOfSize:15];
    [save addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
    [tint addSubview:save];
    
    titlescroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, tint.bottom+15, 120, 140)];
    scroll.bounces = NO;
    [view addSubview:titlescroll];
    
    NSArray *array = [[DataManager sharedManager]getKeyObjects:1];
    for (int i = 0; i<array.count-1; i++) {
        KeyObject *key = [array objectAtIndex:i+1];
        UIButton *type = [UIButton buttonWithType:UIButtonTypeCustom];
        type.frame = CGRectMake(0, 0, 120, 30);
       // type.backgroundColor = [UIColor colorWithRed:180/255.0 green:146/255.0 blue:107/255.0 alpha:1];
        if (i==0) {
            [type setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchbtnselect.png"] forState:UIControlStateNormal];
        }
        else
        {
            [type setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchbtnunselect.png"] forState:UIControlStateNormal];
        }
        type.top = 35*i;
        type.tag = 10+i;
        [type setTitle:key.keyName forState:UIControlStateNormal];
        type.titleLabel.adjustsFontSizeToFitWidth = YES;
        [type addTarget:self action:@selector(selecttype:) forControlEvents:UIControlEventTouchUpInside];
        [titlescroll addSubview:type];
        type.titleLabel.textColor = [UIColor blackColor];
        titlescroll.contentSize = CGSizeMake(120, 35*(i+1));
    }
#else
    view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1024, 360)];
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    UIImage *tintImage = [[UIHelper getInstance] getBundleImage:@"detail_tint.png"];
    tintImage = [tintImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, 16, 0, 16)];
    
    tint = [[UIImageView alloc]initWithImage:tintImage];
    tint.width = 1024;
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0)
    {
        tint.top = 64;
    }
    tint.userInteractionEnabled = YES;
    [view addSubview:tint];
    
    UIButton *clear = [UIButton buttonWithType:UIButtonTypeCustom];
    [clear setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"anniu.png"] forState:UIControlStateNormal];
    clear.frame = CGRectMake(0, 0, 70, 30);
    clear.center = CGPointMake(870, tint.height/2);
    [clear setTitle:@"清除配比" forState:UIControlStateNormal];
    clear.titleLabel.font = [UIFont systemFontOfSize:15];
    [clear addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
    [tint addSubview:clear];
    
    UIButton *save = [UIButton buttonWithType:UIButtonTypeCustom];
    [save setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"anniu.png"] forState:UIControlStateNormal];
    save.frame = CGRectMake(0, 0, 70, 30);
    save.center = CGPointMake(950, tint.height/2);
    [save setTitle:@"保存配比" forState:UIControlStateNormal];
    save.titleLabel.font = [UIFont systemFontOfSize:15];
    [save addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
    [tint addSubview:save];
    
    titlescroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, tint.bottom+15, 120, 210)];
    scroll.bounces = NO;
    [view addSubview:titlescroll];
    
    NSArray *array = [[DataManager sharedManager]getKeyObjects:1];
    for (int i = 0; i<array.count-1; i++) {
        KeyObject *key = [array objectAtIndex:i+1];
        UIButton *type = [UIButton buttonWithType:UIButtonTypeCustom];
        type.frame = CGRectMake(0, 0, 120, 30);
       // type.backgroundColor = [UIColor colorWithRed:180/255.0 green:146/255.0 blue:107/255.0 alpha:1];
        if (i==0) {
            [type setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchbtnselect.png"] forState:UIControlStateNormal];
        }
        else
        {
            [type setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchbtnunselect.png"] forState:UIControlStateNormal];
        }
        type.top = 35*i;
        type.tag = 10+i;
        [type setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [type setTitle:key.keyName forState:UIControlStateNormal];
        type.titleLabel.adjustsFontSizeToFitWidth = YES;
        [type addTarget:self action:@selector(selecttype:) forControlEvents:UIControlEventTouchUpInside];
        [titlescroll addSubview:type];
        titlescroll.contentSize = CGSizeMake(120, 35*(i+1));
    }
    img = [[UIHelper getInstance] getBundleImage:@"page_scrollIndicator.png"];
    UIImageView *scrollindicator = [[UIImageView alloc]initWithImage:img];
    scrollindicator.frame = CGRectMake(0, 0, img.size.width, img.size.height);
    scrollindicator.center = CGPointMake(titlescroll.size.width/2+10, titlescroll.bottom+10);
    [view addSubview:scrollindicator];
#endif
    KeyObject *key = [array objectAtIndex:1];
    typeno = [[DataManager sharedManager]gettypeno:key.keyName];
    [self refreshUI:typeno type:key.keyName childtype:nil];
    isclick=NO;
}
-(void)ret
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)refreshUI:(int)sizegroupno type:(NSString *)typename childtype:(NSString *)childtypename
{
    typestr=typename;
    childtypestr=childtypename;
    [scroll removeFromSuperview];
#ifdef DHH_iOS
    scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(140, tint.bottom+15, 170, 180)];
    scroll.alwaysBounceVertical = YES;
    scroll.alwaysBounceHorizontal = YES;
    scroll.showsHorizontalScrollIndicator = YES;
    scroll.bounces = NO;
    [view addSubview:scroll];
    
    sizegroupid = [[DataManager sharedManager] getSizeListBySizeGroup:sizegroupno SizeType:@""];
    NSArray *sizenum = [[DataManager sharedManager]getmatching:typename childtype:childtypename];
    for (int i = 0; i<sizegroupid.count+1; i++) {
        if (i==0) {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(80*i, 0, 79, 35)];
            label.backgroundColor = [UIColor orangeColor];
            label.textAlignment = NSTextAlignmentCenter;
            [scroll addSubview:label];
            
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(80*(i+1)-1, 0, 1, 35)];
            line.backgroundColor = [UIColor blackColor];
            [scroll addSubview:line];
            scroll.contentSize = CGSizeMake(80*(i+1), 35);
        }
        else
        {
         SizeObject *sizeobject = [sizegroupid objectAtIndex:i-1];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(80*i, 0, 79, 35)];
        label.backgroundColor = [UIColor orangeColor];
        label.text = sizeobject.sSizeName;
        label.textAlignment = NSTextAlignmentCenter;
        [scroll addSubview:label];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(80*(i+1)-1, 0, 1, 35)];
        line.backgroundColor = [UIColor blackColor];
        [scroll addSubview:line];
        scroll.contentSize = CGSizeMake(80*(i+1), 35);
        }
    }
    UIView* dummyView = [[UIView alloc] initWithFrame:CGRectZero];
    for (int i=0; i<3; i++) {
        for (int j = 0; j<sizegroupid.count+1; j++) {
            if (j==0) {
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(80*j, 35+i*35, 79, 34)];
                label.backgroundColor = [UIColor whiteColor];
                label.textAlignment = NSTextAlignmentCenter;
                if (i==0) {
                    label.text = @"尺码组1";
                }
                else if(i==1)
                {
                    label.text = @"尺码组2";
                }
                else if(i==2)
                {
                    label.text = @"尺码组3";
                }
                [scroll addSubview:label];
                
                UIView *line = [[UIView alloc]initWithFrame:CGRectMake(80*(j+1)-1, 35+i*35, 1, 34)];
                line.backgroundColor = [UIColor blackColor];
                [scroll addSubview:line];
            }
            else
            {
                UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(80*j, 35+i*35, 79, 34)];
                textfield.backgroundColor = [UIColor whiteColor];
                textfield.delegate = self;
                if (sizenum.count>0) {
                    textfield.text = [sizenum objectAtIndex:j-1];
                }
                else
                {
                    textfield.text = @"0";
                }
                textfield.tag = 100*i+j;
                textfield.inputView = dummyView;
                textfield.textAlignment = NSTextAlignmentCenter;
                textfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
                [scroll addSubview:textfield];
                
                UIView *line = [[UIView alloc]initWithFrame:CGRectMake(80*(j+1)-1, 35+i*35, 1, 34)];
                line.backgroundColor = [UIColor blackColor];
                [scroll addSubview:line];
            }
        }
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, (sizegroupid.count+1)* 80, 1)];
        line.backgroundColor = [UIColor blackColor];
        line.top = 69+i*35;
        [scroll addSubview:line];
        scroll.contentSize = CGSizeMake(scroll.contentSize.width, 35*(i+2));
    }
#else
    scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(140, tint.bottom+15, 850, 380)];
    scroll.alwaysBounceVertical = YES;
    scroll.alwaysBounceHorizontal = YES;
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.bounces = NO;
    [view addSubview:scroll];
    
    sizegroupid = [[DataManager sharedManager] getSizeListBySizeGroup:sizegroupno SizeType:@""];
    NSArray *sizenum = [[DataManager sharedManager]getmatching:typename childtype:childtypename];
    for (int i = 0; i<sizegroupid.count+1; i++) {
        if (i==0) {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(80*i, 0, 79, 35)];
            label.backgroundColor = [UIColor orangeColor];
            label.textAlignment = NSTextAlignmentCenter;
            [scroll addSubview:label];
            
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(80*(i+1)-1, 0, 1, 35)];
            line.backgroundColor = [UIColor blackColor];
            [scroll addSubview:line];
            scroll.contentSize = CGSizeMake(80*(i+1), 35);
        }
        else
        {
            SizeObject *sizeobject = [sizegroupid objectAtIndex:i-1];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(80*i, 0, 79, 35)];
            label.backgroundColor = [UIColor orangeColor];
            label.text = sizeobject.sSizeName;
            label.textAlignment = NSTextAlignmentCenter;
            [scroll addSubview:label];
            
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(80*(i+1)-1, 0, 1, 35)];
            line.backgroundColor = [UIColor blackColor];
            [scroll addSubview:line];
            scroll.contentSize = CGSizeMake(80*(i+1), 35);
        }
    }
    UIView* dummyView = [[UIView alloc] initWithFrame:CGRectZero];
    for (int i=0; i<1; i++) {
        for (int j = 0; j<sizegroupid.count+1; j++) {
            if (j==0) {
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(80*j, 35+i*35, 79, 34)];
                label.backgroundColor = [UIColor whiteColor];
                label.textAlignment = NSTextAlignmentCenter;
                if (i==0) {
                    label.text = @"尺码组1";
                }
                else if(i==1)
                {
                    label.text = @"尺码组2";
                }
                else if(i==2)
                {
                    label.text = @"尺码组3";
                }
                else if(i==3)
                {
                    label.text = @"尺码组4";
                }
                else if(i==4)
                {
                    label.text = @"尺码组5";
                }
                [scroll addSubview:label];
                
                UIView *line = [[UIView alloc]initWithFrame:CGRectMake(80*(j+1)-1, 35+i*35, 1, 34)];
                line.backgroundColor = [UIColor blackColor];
                [scroll addSubview:line];
            }
            else
            {
                UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(80*j, 35+i*35, 79, 34)];
                textfield.backgroundColor = [UIColor whiteColor];
                textfield.delegate = self;
                if (sizenum.count>0) {
                    textfield.text = [sizenum objectAtIndex:j-1];
                }
                else
                {
                    textfield.text = @"0";
                }
                textfield.tag = 100*i+j;
                textfield.inputView = dummyView;
                textfield.textAlignment = NSTextAlignmentCenter;
                textfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
                [scroll addSubview:textfield];
                
                UIView *line = [[UIView alloc]initWithFrame:CGRectMake(80*(j+1)-1, 35+i*35, 1, 34)];
                line.backgroundColor = [UIColor blackColor];
                [scroll addSubview:line];
            }
        }
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, (sizegroupid.count+1)* 80, 1)];
        line.backgroundColor = [UIColor blackColor];
        line.top = 69+i*35;
        [scroll addSubview:line];
        scroll.contentSize = CGSizeMake(scroll.contentSize.width, 35*(i+2));
    }
    
#endif
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)selecttype:(UIButton *)sender
{
    NSString *keyname = sender.titleLabel.text;
    NSString *groupno = [[DataManager sharedManager]getgroupno:keyname];
    if ([groupno isEqualToString:@"error"]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"数据库有错误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return;
    }
    int groupnonum = [groupno intValue];
    NSArray *childarray = [[DataManager sharedManager]getChildrenKeyObjects:groupnonum];
    if (childarray.count==0) {
        return;
    }
    else if (childarray.count==1)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"该类没有子类" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return;
    }
    if (typetag==sender.tag&&isclick==YES) {
        [titlescroll removeFromSuperview];
#ifdef DHH_iOS
        titlescroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, tint.bottom+15, 120, 140)];
        scroll.bounces = NO;
        [view addSubview:titlescroll];
        
        NSArray *array = [[DataManager sharedManager]getKeyObjects:1];
        for (int i = 0; i<array.count-1; i++) {
            KeyObject *key = [array objectAtIndex:i+1];
            UIButton *type = [UIButton buttonWithType:UIButtonTypeCustom];
            type.frame = CGRectMake(0, 0, 120, 30);
            if (i==sender.tag-10) {
                [type setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchbtnselect.png"] forState:UIControlStateNormal];
            }
            else
            {
                [type setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchbtnunselect.png"] forState:UIControlStateNormal];
            }
            type.top = 35*i;
            type.tag = 10+i;
            [type setTitle:key.keyName forState:UIControlStateNormal];
            type.titleLabel.adjustsFontSizeToFitWidth = YES;
            [type addTarget:self action:@selector(selecttype:) forControlEvents:UIControlEventTouchUpInside];
            [titlescroll addSubview:type];
            [type setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            titlescroll.contentSize = CGSizeMake(120, 35*(i+1));
        }
#else
        titlescroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, tint.bottom+15, 120, 210)];
        scroll.bounces = NO;
        [view addSubview:titlescroll];
        
        NSArray *array = [[DataManager sharedManager]getKeyObjects:1];
        for (int i = 0; i<array.count-1; i++) {
            KeyObject *key = [array objectAtIndex:i+1];
            UIButton *type = [UIButton buttonWithType:UIButtonTypeCustom];
            type.frame = CGRectMake(0, 0, 120, 30);
            if (i==sender.tag-10) {
                [type setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchbtnselect.png"] forState:UIControlStateNormal];
            }
            else
            {
                [type setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchbtnunselect.png"] forState:UIControlStateNormal];
            }
            type.top = 35*i;
            type.tag = 10+i;
            [type setTitle:key.keyName forState:UIControlStateNormal];
            type.titleLabel.adjustsFontSizeToFitWidth = YES;
            [type addTarget:self action:@selector(selecttype:) forControlEvents:UIControlEventTouchUpInside];
            [titlescroll addSubview:type];
            [type setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            titlescroll.contentSize = CGSizeMake(120, 35*(i+1));
        }
#endif
        titlescroll.contentOffset = CGPointMake(0, 35*(sender.tag-10));
        isclick=NO;
    }
    else
    {
        typetag=sender.tag;
        typestr = sender.titleLabel.text;
        [titlescroll removeFromSuperview];
#ifdef DHH_iOS
        titlescroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, tint.bottom+15, 120, 140)];
        scroll.bounces = NO;
        [view addSubview:titlescroll];
#else
        titlescroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, tint.bottom+15, 120, 210)];
        scroll.bounces = NO;
        [view addSubview:titlescroll];
#endif
        NSArray *array = [[DataManager sharedManager]getKeyObjects:1];
        for (int i = 0; i<sender.tag-9; i++) {
            KeyObject *key = [array objectAtIndex:i+1];
            UIButton *type = [UIButton buttonWithType:UIButtonTypeCustom];
            type.frame = CGRectMake(0, 0, 120, 30);
            [type setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchbtnunselect.png"] forState:UIControlStateNormal];
            type.top = 35*i;
            type.tag = 10+i;
            [type setTitle:key.keyName forState:UIControlStateNormal];
            type.titleLabel.adjustsFontSizeToFitWidth = YES;
            [type addTarget:self action:@selector(selecttype:) forControlEvents:UIControlEventTouchUpInside];
            [titlescroll addSubview:type];
            [type setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            titlescroll.contentSize = CGSizeMake(120, 35*(i+1));
        }
        NSArray *childarray = [[DataManager sharedManager]getChildrenKeyObjects:groupnonum];
        for (int i=0; i<childarray.count-1; i++) {
            KeyObject *key = [childarray objectAtIndex:i+1];
            UIButton *type = [UIButton buttonWithType:UIButtonTypeCustom];
            type.frame = CGRectMake(5, 0, 110, 25);
            if (i==0) {
                childbtntag = 100*(sender.tag-9)+10;
                [type setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchchildbtnselect.png"] forState:UIControlStateNormal];
            }
            else
            {
                [type setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchchildbtnunselect.png"] forState:UIControlStateNormal];
            }
            type.top = 35*(sender.tag-9)+30*i;
            type.tag = 100*(sender.tag-9)+10+i;
            [type setTitle:key.keyName forState:UIControlStateNormal];
            type.titleLabel.adjustsFontSizeToFitWidth = YES;
            type.titleLabel.font=[UIFont systemFontOfSize:12];
            [type addTarget:self action:@selector(selectchildtype:) forControlEvents:UIControlEventTouchUpInside];
            [titlescroll addSubview:type];
            [type setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            titlescroll.contentSize = CGSizeMake(120, 35*(sender.tag-9)+30*(i+1));
        }
        for (int i=sender.tag-9; i<array.count-1; i++) {
            KeyObject *key = [array objectAtIndex:i+1];
            UIButton *type = [UIButton buttonWithType:UIButtonTypeCustom];
            type.frame = CGRectMake(0, 0, 120, 30);
            [type setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchbtnunselect.png"] forState:UIControlStateNormal];
            type.top = 35*i+30*(childarray.count-1);
            type.tag = 10+i;
            [type setTitle:key.keyName forState:UIControlStateNormal];
            type.titleLabel.adjustsFontSizeToFitWidth = YES;
            [type addTarget:self action:@selector(selecttype:) forControlEvents:UIControlEventTouchUpInside];
            [titlescroll addSubview:type];
            [type setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            titlescroll.contentSize = CGSizeMake(120, 35*(i+1)+30*(childarray.count-1));
        }
        titlescroll.contentOffset = CGPointMake(0, 35*(sender.tag-10));
        isclick=YES;
    }
    if (childarray.count>1) {
        UIButton *btn = (UIButton *)[titlescroll viewWithTag:childbtntag];
        [self selectchildtype:btn];
       // KeyObject *key = [childarray objectAtIndex:1];
       // [self refreshUI:groupnonum type:keyname childtype:key.keyName];
    }
}
-(void)selectchildtype:(UIButton *)sender
{
    UIButton *btn = (UIButton *)[titlescroll viewWithTag:childbtntag];
    [btn setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchchildbtnunselect.png"] forState:UIControlStateNormal];
    [sender setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"matchchildbtnselect.png"] forState:UIControlStateNormal];
    childbtntag=sender.tag;
    
    childtypestr=sender.titleLabel.text;
    typeno = [[DataManager sharedManager]gettypeno:childtypestr];
    [self refreshUI:typeno type:typestr childtype:childtypestr];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    edittext = textField;
    [textField setBackground:[[UIHelper getInstance] getBundleImage:@"order_click.png"]];
    if ([textField.text isEqualToString:@"0"])
    {
        textField.text = @"";
    }
    text = textField;
    NSString *str = text.text;
    [[NSNotificationCenter defaultCenter]postNotificationName:@"clear" object:str];
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    textField.background = [[UIHelper getInstance] getBundleImage:@"clear.png"];
    if ([textField.text isEqualToString:@""]) {
        textField.text = @"0";
    }
}

-(void)numberInputViewDidClick:(NSString *)clickValue
{
    text.text = clickValue;
}

- (void)numberInputViewEnterClick:(NSString *)clickValue{
    
}
#pragma mark 把原来的编辑框背景去掉
-(void)search:(NSNotification *)sender
{
    UITextField *text1 = [[UITextField alloc]init];

    if (edittext.tag<1000) {
        text1 = (UITextField *)[scroll viewWithTag:edittext.tag+1];
    }

    if (text1) {
        [self textFieldDidEndEditing:edittext];
        [text1 becomeFirstResponder];
        [self textFieldDidBeginEditing:text1];
    }
}
-(void)clear:(UIButton *)sender
{
    [edittext resignFirstResponder];
    for (int i=0; i<5; i++) {
        for (int j = 0; j<sizegroupid.count; j++){
            UITextField *textfield = (UITextField *)[scroll viewWithTag:100*i+j+1];
            textfield.text=@"0";
        }
    }
}
-(void)save:(UIButton *)sender
{
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    for (int i=0; i<sizegroupid.count; i++) {
        UITextField *textfield=(UITextField *)[scroll viewWithTag:i+1];
        SizeObject *sizeobject = [sizegroupid objectAtIndex:i];
        if ([textfield.text isEqualToString:@""]) {
            [dic setObject:@"0" forKey:sizeobject.sSizeName];
        }
        else
        {
            [dic setObject:textfield.text forKey:sizeobject.sSizeName];
        }
    }
    [[DataManager sharedManager]setmatching:typestr childtype:childtypestr typeno:typeno num:dic sizearr:sizegroupid];
    showMessage(@"保存成功");
}
@end
